package cn.smbms.service;

import cn.smbms.mapper.UserMapper;
import cn.smbms.pojo.User;
import cn.smbms.pojo.vo.UserListVO;
import cn.smbms.util.PageBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author Tequila
 **/
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public User login(String userCode, String userPassword) {
        return userMapper.login( userCode,  userPassword);
    }

    @Override
    public PageBean<User> findPage(UserListVO vo) {
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setPageNo(vo.getPageIndex());
        pageBean.setPageSize(vo.getPageSize());
        vo.setStartIndex(pageBean.getStartIndex());
        // 根据条件动态查询
        pageBean.setTotalCount(userMapper.findCount(vo));
        pageBean.setResult(userMapper.findUserList(vo));
        return pageBean;
    }

    @Override
    public User findUser(Integer id) {
        return userMapper.findUser(id);
    }

    @Override
    @Transactional
    public boolean updateUser(User user) {
        if (userMapper.updateUser(user) > 0){
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    @Override
    @Transactional
    public boolean addUser(User user) {
        if (userMapper.addUser(user) > 0){
            return true;
        }
        return false;
    }
}
